package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.WithdrawP;
import com.cljz.makepassenger.widget.CodeEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/6/6.
 */

public class WithdrawActivity extends BaseActivity<WithdrawP> {
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_withdraw_all)
    TextView tvWithdrawAll;
    @BindView(R.id.ll_all_money)
    LinearLayout llAllMoney;
    @BindView(R.id.tv_then_money)
    TextView tvThenMoney;
    @BindView(R.id.btn_withdraw_submit)
    Button btnWithdrawSubmit;
    @BindView(R.id.tv_pay_id)
    TextView tvPayID;
    private PopupWindow editorPaymentCodePopWindow;
    private String allMoney;
    private String money;
    private boolean moneyIsOk = false;
    public static final int WEIXIN = 1;
    public static final int ALIPAY = 0;
    private int type = -1;
    private String unionId;
    private String alipayId;
    private String userName;


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("提现");
        btnWithdrawSubmit.setEnabled(false);
        allMoney = getIntent().getExtras().getString("allMoney", "0");
        tvAllMoney.setText(allMoney);

        tvPayID.setText("请选择账户");
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(WithdrawActivity.this);
                    }
                });
        RxView.clicks(tvPayID)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent(WithdrawActivity.this).to(PaySetActivity.class).requestCode(110).launch();
                    }
                });
        RxView.clicks(tvWithdrawAll)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        etMoney.setText(allMoney);
                        etMoney.setSelection(allMoney.length());
                    }
                });
        RxView.clicks(btnWithdrawSubmit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {


                    @Override
                    public void call(Void aVoid) {
                        money = etMoney.getText().toString().toString();
                        showEditorPaymentCodePopWindow();
                    }
                });
        RxTextView.textChanges(etMoney)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 0) {
                            btnWithdrawSubmit.setEnabled(false);
                            llAllMoney.setVisibility(View.VISIBLE);
                            tvThenMoney.setVisibility(View.GONE);
                            return;
                        }
                        if (Float.parseFloat(charSequence.toString()) > Float.parseFloat(allMoney)) {
                            llAllMoney.setVisibility(View.GONE);
                            btnWithdrawSubmit.setEnabled(false);
                            tvThenMoney.setVisibility(View.VISIBLE);
                            tvThenMoney.setText("输入金额超出余额");
                        } else if (Float.parseFloat(charSequence.toString()) < 1.0f) {
                            llAllMoney.setVisibility(View.GONE);
                            btnWithdrawSubmit.setEnabled(false);
                            tvThenMoney.setVisibility(View.VISIBLE);
                            tvThenMoney.setText("提现金额必须大于1.00元");
                        } else {
                            llAllMoney.setVisibility(View.VISIBLE);
                            tvThenMoney.setVisibility(View.GONE);
                            moneyIsOk = true;
                            changeBtnState();
                        }
                    }
                });
    }

    private void showEditorPaymentCodePopWindow() {
        View editorPaymentCodeView = View.inflate(App.getInstance(), R.layout.pop_edit_payment_code, null);
        FrameLayout flPaymentCodeEditorClose = (FrameLayout) editorPaymentCodeView.findViewById(R.id.fl_payment_code_editor_close);
        TextView tvWithdrawMoney = (TextView) editorPaymentCodeView.findViewById(R.id.tv_withdraw_money);
        CodeEditText cetPaymentCode = (CodeEditText) editorPaymentCodeView.findViewById(R.id.cet_payment_code);
        editorPaymentCodePopWindow = new PopupWindow(editorPaymentCodeView,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                true);
        editorPaymentCodePopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        editorPaymentCodePopWindow.setFocusable(true);
        editorPaymentCodePopWindow.setOutsideTouchable(true);
        editorPaymentCodePopWindow.setTouchable(true);
        editorPaymentCodePopWindow.update();
        editorPaymentCodePopWindow.showAtLocation(llMain, Gravity.CENTER, 0, 0);
        tvWithdrawMoney.setText("￥" + etMoney.getText().toString().trim());
        RxView.clicks(flPaymentCodeEditorClose)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        editorPaymentCodePopWindow.dismiss();
                    }
                });
        RxTextView.textChanges(cetPaymentCode)
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 6) {
                            String psd = charSequence.toString().trim();
                            if (type == WEIXIN) {
                                getP().withDrawWeixin(money, unionId, psd);
                            } else {
                                getP().withDrawaAlipay(money, psd);
                            }
                            showLoadingDialog();
                        }
                    }
                });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 110 && resultCode == RESULT_OK) {
            type = data.getIntExtra("type", -1);
            unionId = data.getStringExtra("unionId");
            alipayId = data.getStringExtra("id");
            userName = data.getStringExtra("name");

            if (type != -1) {
                changeBtnState();
                tvPayID.setText(type == WEIXIN ? "微信" : "支付宝");
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (editorPaymentCodePopWindow != null && editorPaymentCodePopWindow.isShowing()) {
            editorPaymentCodePopWindow.dismiss();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public WithdrawP newP() {
        return new WithdrawP();
    }

    public void withDrawaFailed(String error) {
        dismissLoadingDialog();
        showToast(error, R.mipmap.jinshi);
    }

    public void withDrawaSuccess() {
        dismissLoadingDialog();
        Router.newIntent((Activity) App.getInstance())
                .to(WithdrawDetailsActivity.class)
                .putString("money", money)
                .putString("payId", type == WEIXIN ? "微信" : "支付宝")
                .launch();
        Router.pop(WithdrawActivity.this);
    }

    private void changeBtnState() {
        if (moneyIsOk && type != -1) {
            btnWithdrawSubmit.setEnabled(true);
        } else {
            btnWithdrawSubmit.setEnabled(false);
        }
    }
}
